package com.whu.tcx01.hd.calNewRoadMerge;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.solr.common.util.Hash;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class RoadReducer extends Reducer<Text, RoadBean, Text, OutRoadBean> {

    // private OutRoadBean outV = new OutRoadBean();
    private OutRoadBean outV = new OutRoadBean();

    @Override
    protected void reduce(Text key, Iterable<RoadBean> values, Context context) throws IOException, InterruptedException {

        String[] split = key.toString().split("\t");
        if(split[0].equals(split[1])){
            return;
        }

        HashMap<Integer, Long> map = new HashMap<>();

        for (RoadBean value : values) {
            int direction = value.getDirection();
            map.put(direction, map.getOrDefault(direction, 0L) + 1);
        }

        outV.setRoadid(MyConstants.count);
        outV.setStart(Long.parseLong(split[0]));
        outV.setStop(Long.parseLong(split[1]));
        outV.setMap(map);
        context.write(null, outV);
    }
}
